package com.wzy.Controller;

import com.wzy.entity.Battle;
import com.wzy.entity.Blog;
import com.wzy.entity.Hero;
import com.wzy.entity.Violence;
import com.wzy.entity.Vo.DetailVo;
import com.wzy.service.BattleService;
import com.wzy.service.BlogService;
import com.wzy.service.HeroService;
import com.wzy.service.ViolenceService;
import com.wzy.util.textTranfromHtmlUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @program: Anti-JapaneseWarMemorialNetwork
 * @description: 博客详情页展示
 * @author: @why
 * @create: 2021-02-22 15:20
 **/
@Slf4j
@Controller
public class DetailController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private HeroService heroService;

    @Autowired
    private BattleService battleService;

    @Autowired
    private ViolenceService violenceService;


    /**
     * 跳转到详情页面,未进行数据渲染
     *
     * @return
     */
    @GetMapping("/detail")
    public String routerDetailPage(DetailVo detailVo,
                                   Model model) {
        Long blogId = null;
        Long heroId = null;
        Long battleId = null;
        String violenceId = null;
        if (detailVo.getBlogId() != null) {
            blogId = Long.valueOf(detailVo.getBlogId());
        } else if (detailVo.getHeroId() != null) {
            heroId = Long.valueOf(detailVo.getHeroId());
        } else if (detailVo.getBattleId() != null) {
            battleId = Long.valueOf(detailVo.getBattleId());
        } else if (detailVo.getViolenceId() != null) {
            violenceId = detailVo.getViolenceId();
        } else {
            return "index";
        }

        //如果传递过来的是blogId，查blog的信息,传递过来的是heroId查hero的信息
        if (blogId != null) {
            Blog blog = blogService.getBlogById(blogId);
            log.debug("详情页得到的blog" + blog);
            model.addAttribute("blog", blog);
            model.addAttribute("hero", null);
            return "detail";
            //查Hero信息
        } else if (heroId != null) {
            Hero hero = heroService.getHeroById(heroId);
            model.addAttribute("hero", hero);
            model.addAttribute("blog", null);
            return "herodetail";
            //根据战役id查询战役的详情
        } else if (battleId != null) {
            Blog battleDetail = battleService.selectBattleDetailById(battleId);
            log.debug("战役详情" + battleDetail);
            model.addAttribute("hero", null);
            model.addAttribute("blog", battleDetail);
            return "detail";
            //根据id查询暴行的详情
        } else if (violenceId != null) {
            Blog violenceDetail = violenceService.getViolenceDetailById(violenceId);
            model.addAttribute("hero", null);
            model.addAttribute("blog", violenceDetail);
            log.debug("暴行详情" + violenceDetail);
            return "detail";
        } else {
            return "index";
        }


    }


}
